package org.example.lotterysystem.service.activitystatus.operater;

import org.example.lotterysystem.dao.dataobject.ActivityDO;
import org.example.lotterysystem.dao.mapper.ActivityMapper;
import org.example.lotterysystem.dao.mapper.ActivityPrizeMapper;
import org.example.lotterysystem.service.dto.ConvertActivityStatusDTO;
import org.example.lotterysystem.service.enmus.ActivityPrizeStatusEnum;
import org.example.lotterysystem.service.enmus.ActivityStatusEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class ActivityOperator extends AbstractActivityOperator{
    @Autowired
    private ActivityMapper activityMapper;
    @Autowired
    private ActivityPrizeMapper activityPrizeMapper;
    @Override
    public Integer sequence() {
        return 2;
    }

    /**
     * 判断活动是否需要转换
     * @param convertActivityStatusDTO
     * @return
     */
    @Override
    public Boolean needConvert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        Long activityId = convertActivityStatusDTO.getActivityId();
        ActivityStatusEnum targetActivityStatus = convertActivityStatusDTO.getTargetActivityStatus();
        if(activityId==null||null==targetActivityStatus){
            return false;
        }
        //查询活动是否存在
        ActivityDO activityDO = activityMapper.selectById(activityId);
        if (null==activityDO){
            return false;
        }
        //当前活动状态与传入的状态是否一致，一致则不处理
        if(targetActivityStatus.name().equalsIgnoreCase(activityDO.getStatus())){
            return false;
        }

        //判断奖品是否全部抽取
        //查询INIT状态的奖品个数
        int count=activityPrizeMapper.countPrize(activityId, ActivityPrizeStatusEnum.INIT.name());
        if (count>0){
            return false;
        }
        return true;
    }

    /**
     * 转换活动，更新数据库
     * @param convertActivityStatusDTO
     * @return
     */
    @Override
    public Boolean convert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        try{
            activityMapper.updateStatus(convertActivityStatusDTO.getActivityId(),convertActivityStatusDTO.getTargetActivityStatus().name());
            return  true;
        }catch (Exception e){
            return false;
        }

    }
}
